package com.cjh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjh.common.CustomException;
import com.cjh.entity.Category;
import com.cjh.entity.Dish;
import com.cjh.entity.Setmeal;
import com.cjh.service.CategoryService;
import com.cjh.mapper.CategoryMapper;
import com.cjh.service.DishService;
import com.cjh.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author 非鱼
* @description 针对表【category(菜品及套餐分类)】的数据库操作Service实现
* @createDate 2022-08-17 15:36:09
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{
    @Resource
    private DishService dishService;
    @Resource
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {

        LambdaQueryWrapper<Dish> dishWrapper=new LambdaQueryWrapper<>();
        dishWrapper.eq(Dish::getCategoryId,id);
        int count1=dishService.count(dishWrapper);
        if(count1>0){
            throw new CustomException("该分类关联菜品,不可以删除!!!");
        }


        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2=setmealService.count(setmealLambdaQueryWrapper);
        if(count2>0){
            throw new CustomException("该分类关联套餐,不可以删除!!!");
        }
        super.removeById(id);
    }
}




